<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="公司编码" prop="companyCode">
        <el-input
          v-model="queryParams.companyCode"
          placeholder="请输入公司编码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="公司代码" prop="viewCode">
        <el-input
          v-model="queryParams.viewCode"
          placeholder="请输入公司代码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="公司名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="区域编码" prop="areaCode">
        <el-input
          v-model="queryParams.areaCode"
          placeholder="请输入区域编码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="所在区域" prop="localAreaCode">
        <el-input
          v-model="queryParams.localAreaCode"
          placeholder="请输入所在区域"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="行业类型（字典industry_type）" prop="industryType">
        <el-select
          v-model="queryParams.industryType"
          placeholder="请选择行业类型（字典industry_type）"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="行业部门" prop="industryDept">
        <el-input
          v-model="queryParams.industryDept"
          placeholder="请输入行业部门"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="公司类型（字典company_type）" prop="companyType">
        <el-select
          v-model="queryParams.companyType"
          placeholder="请选择公司类型（字典company_type）"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="监管类别（字典unit_monitor_type）" prop="unitType">
        <el-select
          v-model="queryParams.unitType"
          placeholder="请选择监管类别（字典unit_monitor_type）"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="对接app_id" prop="appId">
        <el-input
          v-model="queryParams.appId"
          placeholder="请输入对接app_id"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="第三方数据id" prop="dataId">
        <el-input
          v-model="queryParams.dataId"
          placeholder="请输入第三方数据id"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['cfs:sys-company:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['cfs:sys-company:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
        <el-button
            type="danger"
            plain
            :disabled="isEmpty(checkedIds)"
            @click="handleDeleteBatch"
            v-hasPermi="['cfs:sys-company:delete']"
        >
          <Icon icon="ep:delete" class="mr-5px" /> 批量删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
        row-key="id"
        v-loading="loading"
        :data="list"
        :stripe="true"
        :show-overflow-tooltip="true"
        @selection-change="handleRowCheckboxChange"
    >
    <el-table-column type="selection" width="55" />
      <el-table-column label="公司名称" width="300" align="center" prop="companyName" />
      <el-table-column label="区域编码" align="center" prop="areaCode" />
      <el-table-column label="所在区域" align="center" prop="localAreaCode" />
      <el-table-column label="行业类型（字典industry_type）" align="center" prop="industryType" />
      <el-table-column label="行业部门" align="center" prop="industryDept" />
      <el-table-column label="公司类型（字典company_type）" align="center" prop="companyType" />
      <el-table-column label="监管类别（字典unit_monitor_type）" align="center" prop="unitType" />
      <el-table-column label="对接app_id" align="center" prop="appId" />
      <el-table-column label="第三方数据id" align="center" prop="dataId" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.companyCode)"
            v-hasPermi="['cfs:sys-company:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['cfs:sys-company:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <SysCompanyForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { SysCompanyApi, SysCompany } from '@/api/cfs/company'
import SysCompanyForm from './SysCompanyForm.vue'

/** sys_company 列表 */
defineOptions({ name: 'SysCompany' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<SysCompany[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  companyCode: undefined,
  treeSort: undefined,
  viewCode: undefined,
  companyName: undefined,
  fullName: undefined,
  areaCode: undefined,
  localAreaCode: undefined,
  industryType: undefined,
  industryDept: undefined,
  companyType: undefined,
  unitType: undefined,
  lng: undefined,
  lat: undefined,
  appId: undefined,
  dataId: undefined,
  provId: undefined,
  cityId: undefined,
  countyId: undefined,
  townId: undefined,
  villageId: undefined,
  gridId: undefined,
  status: undefined,
  createTime: [],
  createUser: undefined,
  updateUser: undefined,
  remarks: undefined,
  provName: undefined,
  cityName: undefined,
  countyName: undefined,
  townName: undefined,
  villageName: undefined,
  gridName: undefined,
  networkStatus: undefined,
  fireSafePerson: undefined,
  fireSafePersonTel: undefined,
  orgLicense: undefined,
  address: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await SysCompanyApi.getSysCompanyPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await SysCompanyApi.deleteSysCompany(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 批量删除sys_company */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await SysCompanyApi.deleteSysCompanyList(checkedIds.value);
    message.success(t('common.delSuccess'))
    await getList();
  } catch {}
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: SysCompany[]) => {
  checkedIds.value = records.map((item) => item.id);
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await SysCompanyApi.exportSysCompany(queryParams)
    download.excel(data, 'sys_company.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
